﻿using System;
using System.ComponentModel;

namespace HalconDotNet
{
	// Token: 0x0200008A RID: 138
	public class HHandleBase : IDisposable
	{
		// Token: 0x06001E0A RID: 7690 RVA: 0x00020CE0 File Offset: 0x0001EEE0
		internal HHandleBase() : this(HHandleBase.UNDEF)
		{
		}

		// Token: 0x06001E0B RID: 7691 RVA: 0x000B5643 File Offset: 0x000B3843
		[EditorBrowsable(EditorBrowsableState.Never)]
		internal HHandleBase(IntPtr handle)
		{
			this.Handle = handle;
		}

		// Token: 0x06001E0C RID: 7692 RVA: 0x000B5652 File Offset: 0x000B3852
		private void SetHandleInternal(IntPtr handle, bool copy)
		{
			this.ClearHandleInternal();
			if (this.suppressedFinalization)
			{
				this.suppressedFinalization = false;
				GC.ReRegisterForFinalize(this);
			}
			if (handle != HHandleBase.UNDEF)
			{
				this.mHandle = (copy ? HalconAPI.CopyHandle(handle) : handle);
			}
		}

		// Token: 0x06001E0D RID: 7693 RVA: 0x000B568E File Offset: 0x000B388E
		internal HHandleBase(HHandleBase handle)
		{
			this.SetHandleInternal(handle, true);
		}

		/// <summary>Returns the HALCON ID for the handle</summary>
		/// <remarks>Caller must ensure that input handle is kept alive</remarks>
		// Token: 0x1700006E RID: 110
		// (get) Token: 0x06001E0E RID: 7694 RVA: 0x000B56A3 File Offset: 0x000B38A3
		// (set) Token: 0x06001E0F RID: 7695 RVA: 0x000B56AB File Offset: 0x000B38AB
		[EditorBrowsable(EditorBrowsableState.Never)]
		public IntPtr Handle
		{
			get
			{
				return this.mHandle;
			}
			set
			{
				this.SetHandleInternal(value, true);
				HalconAPI.ReleaseExternalOwnership(value);
			}
		}

		/// <summary>Relinquish ownership of managed handle</summary>
		/// <remarks>
		///   Caller must ensure that handle is destroyed at some time
		///   after this instance is no longer used.
		/// </remarks>
		// Token: 0x06001E10 RID: 7696 RVA: 0x000B56BC File Offset: 0x000B38BC
		[EditorBrowsable(EditorBrowsableState.Never)]
		public void Detach()
		{
			HalconAPI.AcquireExternalOwnership(this.mHandle);
		}

		/// <summary>
		///   Returns true if the handle has been initialized.
		/// </summary>
		/// <remarks>
		///   A handle will be uninitialized when creating it with a
		///   no-argument constructor or after calling Dispose();
		/// </remarks>
		// Token: 0x06001E11 RID: 7697 RVA: 0x000B56CA File Offset: 0x000B38CA
		public bool IsInitialized()
		{
			return HalconAPI.HandleIsValid(this.mHandle);
		}

		// Token: 0x06001E12 RID: 7698 RVA: 0x000B56D8 File Offset: 0x000B38D8
		~HHandleBase()
		{
			try
			{
				this.Dispose(false);
			}
			catch (Exception)
			{
			}
		}

		// Token: 0x06001E13 RID: 7699 RVA: 0x000B5714 File Offset: 0x000B3914
		private void Dispose(bool disposing)
		{
			if (this.mHandle != HHandleBase.UNDEF)
			{
				this.ClearHandleInternal();
				this.mHandle = HHandleBase.UNDEF;
			}
			if (disposing)
			{
				GC.SuppressFinalize(this);
				this.suppressedFinalization = true;
			}
			GC.KeepAlive(this);
		}

		// Token: 0x06001E14 RID: 7700 RVA: 0x000B574F File Offset: 0x000B394F
		void IDisposable.Dispose()
		{
			this.Dispose(true);
		}

		/// <summary>Releases the resources used by this handle object</summary>
		// Token: 0x06001E15 RID: 7701 RVA: 0x000B574F File Offset: 0x000B394F
		public virtual void Dispose()
		{
			this.Dispose(true);
		}

		/// <summary>
		///   Invalidates the handle but keeps the HALCON handle alive, which
		///   only makes sense if the handle is used externally and cleared later,
		///   e.g. by an HOperatorSet based module or another language interface.
		/// </summary>
		// Token: 0x06001E16 RID: 7702 RVA: 0x000B5758 File Offset: 0x000B3958
		[EditorBrowsable(EditorBrowsableState.Never)]
		public void InvalidateWithoutDispose()
		{
			this.Detach();
			this.Dispose();
		}

		// Token: 0x06001E17 RID: 7703 RVA: 0x000B5766 File Offset: 0x000B3966
		internal void Store(IntPtr proc, int parIndex)
		{
			HalconAPI.StoreH(proc, parIndex, this.mHandle);
		}

		// Token: 0x06001E18 RID: 7704 RVA: 0x000B5778 File Offset: 0x000B3978
		internal int Load(IntPtr proc, int parIndex, int err)
		{
			if (this.mHandle != HHandleBase.UNDEF)
			{
				throw new HalconException("Undisposed handle instance when loading output parameter");
			}
			if (HalconAPI.IsFailure(err))
			{
				return err;
			}
			HHandle hhandle;
			err = HalconAPI.LoadH(proc, parIndex, err, out hhandle);
			this.SetHandleInternal(hhandle.Handle, true);
			hhandle.Dispose();
			return err;
		}

		// Token: 0x06001E19 RID: 7705 RVA: 0x000B57CC File Offset: 0x000B39CC
		protected virtual void ClearHandleInternal()
		{
			if (this.mHandle != HHandleBase.UNDEF)
			{
				HalconAPI.ClearHandle(this.mHandle);
				this.mHandle = HHandleBase.UNDEF;
			}
		}

		/// <summary>
		///   Provides a simple string representation of the handle id
		///   as hex number, which is mainly useful for debug outputs (to
		///   see if two handles are identical)
		/// </summary>
		// Token: 0x06001E1A RID: 7706 RVA: 0x000B57F8 File Offset: 0x000B39F8
		public override string ToString()
		{
			if (this.mHandle == HHandleBase.UNDEF)
			{
				return "";
			}
			return "H" + this.mHandle.ToInt64().ToString("X");
		}

		// Token: 0x06001E1B RID: 7707 RVA: 0x000B5840 File Offset: 0x000B3A40
		protected internal void AssertSemType(string sem_type)
		{
			if (!HalconAPI.IsLegacyHandleMode() && this.mHandle != HHandleBase.UNDEF)
			{
				string handleSemType = HalconAPI.GetHandleSemType(this.mHandle);
				if (!sem_type.Equals(handleSemType))
				{
					throw new HalconException("Invalid handle instance passed");
				}
			}
			GC.KeepAlive(this);
		}

		// Token: 0x06001E1C RID: 7708 RVA: 0x000B588C File Offset: 0x000B3A8C
		[EditorBrowsable(EditorBrowsableState.Never)]
		public static HTuple ConcatArray(HHandleBase[] handles)
		{
			return new HTuple(handles as HHandle[]);
		}

		/// <summary>Cast to IntPtr returns HALCON ID of handle resources</summary>
		/// <remarks>Caller must ensure that input object is kept alive</remarks>
		// Token: 0x06001E1D RID: 7709 RVA: 0x000B5899 File Offset: 0x000B3A99
		public static implicit operator IntPtr(HHandleBase handle)
		{
			if (handle == null)
			{
				return HHandleBase.UNDEF;
			}
			return handle.mHandle;
		}

		/// <summary>Represents an uninitialized handle instance</summary>
		// Token: 0x0400115A RID: 4442
		[EditorBrowsable(EditorBrowsableState.Never)]
		public static readonly IntPtr UNDEF = IntPtr.Zero;

		// Token: 0x0400115B RID: 4443
		[EditorBrowsable(EditorBrowsableState.Never)]
		public static readonly HHandle HNULL = new HHandle();

		// Token: 0x0400115C RID: 4444
		private IntPtr mHandle;

		// Token: 0x0400115D RID: 4445
		private bool suppressedFinalization;
	}
}
